package com.iphanta.community.user.web;

import com.baidu.fsg.uid.UidGenerator;
import com.iphanta.community.common.BusinessException;
import com.iphanta.community.common.UserUtils;
import com.iphanta.community.common.entity.DataResult;
import com.iphanta.community.common.message.CommunityMessage;
import com.iphanta.community.common.utils.ResultUtils;
import com.iphanta.community.remote.common.RemoteResult;
import com.iphanta.community.remote.feign.UserCenterFeign;
import com.iphanta.community.remote.feign.VerifyTokenFeign;
import com.iphanta.community.remote.service.TimService;
import com.iphanta.community.remote.traffic.GetPlayerInfoRequest;
import com.iphanta.community.remote.traffic.GetPlayerInfoResponse;
import com.iphanta.community.remote.traffic.VerifyTokenRequest;
import com.iphanta.community.user.mapper.UserBasicInfoMapper;
import com.iphanta.community.user.service.TestUserService;
import com.iphanta.community.user.service.UserService;
import com.iphanta.community.user.traffic.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 用户相关接口
 *
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 2/22/22 11:23 AM
 */
@RestController
@RequestMapping("community/user/")
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private TestUserService testUserService;

    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @PostMapping("register")
    public DataResult<UserRegisterResponse> register(@RequestBody UserRegisterRequest request) {
        return ResultUtils.SUCCESS.result(this.service.register(request));
    }

    /**
     * 用户修改
     *
     * @param request
     * @return
     */
    @PostMapping("modify")
    public DataResult<UserModifyResponse> modify(@RequestBody UserModifyRequest request) {
        return ResultUtils.SUCCESS.result(this.service.modify(request));
    }

    /**
     * 用户信息
     *
     * @param request
     * @return
     */
    @PostMapping("info")
    public DataResult<UserInfoResponse> info(@RequestBody UserInfoRequest request) {
        return ResultUtils.SUCCESS.result(this.service.info(request));
    }

    /**
     * 用户通知
     *
     * @param request
     * @return
     */
    @PostMapping("notification")
    public DataResult<UserNotificationResponse> notification(@RequestBody UserNotificationRequest request) {
        return ResultUtils.SUCCESS.result(this.service.notification(request));
    }

    /**
     * 用户通知是否已读
     *
     * @return
     */
    @GetMapping("isRead")
    public DataResult<UserNotificationReadResponse> isRead() {
        return ResultUtils.SUCCESS.result(this.service.isRead());
    }

    @Autowired
    private VerifyTokenFeign verifyTokenFeign;

    @Autowired
    private UserCenterFeign userCenterFeign;

    @Autowired
    private TimService timService;

    @PostMapping("sendMessage")
    public DataResult sendMessage(@RequestBody CommunityMessage message) {
        this.timService.sendMessage("hand-pick-1002", UserUtils.getUid(), message);
        return ResultUtils.SUCCESS.result();
    }

    /**
     * 用户登陆校验
     */
    @PostMapping("authorization")
    public DataResult<UserAuthorizationResponse> authorization(@RequestBody UserAuthorizationRequest request) {
        UserAuthorizationResponse response = new UserAuthorizationResponse();
        response.setToken(request.getToken());
        GetPlayerInfoRequest getPlayerInfoRequest = new GetPlayerInfoRequest();
        getPlayerInfoRequest.setToken(request.getToken());
        getPlayerInfoRequest.setApiId(2);
        RemoteResult<GetPlayerInfoResponse> remoteResult = userCenterFeign.getPlayerInfo(getPlayerInfoRequest);
        if (!Objects.equals(remoteResult.getCode(), 0)) {
            throw new BusinessException(remoteResult.getMsg(), ResultUtils.FAIL_ON_BUSINESS.result());
        }
        GetPlayerInfoResponse getPlayerInfoResponse = remoteResult.getData();
        getPlayerInfoResponse.getLv();
        return ResultUtils.SUCCESS.result(response);
    }

    @Autowired
    private UserBasicInfoMapper userBasicInfoMapper;
    @Autowired
    private UidGenerator uidGenerator;

//    /**
//     * 用户登陆校验
//     */
//    @PostMapping("test")
//    public DataResult<UserAuthorizationResponse> test(@RequestBody UserAuthorizationRequest request) {
//        UserAuthorizationResponse response = new UserAuthorizationResponse();
//        response.setToken(request.getToken());
//
//
//        List<String> s = this.userBasicInfoMapper.test();
//        for(String sql : s) {
//            System.out.println(String.format(sql, uidGenerator.getUID()));
//        }
//
//        return ResultUtils.SUCCESS.result(response);
//    }
}
